package io.admin.modules.sys.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.admin.common.page.PageData;
import io.admin.common.service.impl.CrudServiceImpl;
import io.admin.common.utils.ConvertUtils;
import io.admin.modules.sys.dao.EquipmentDao;
import io.admin.modules.sys.dao.FileUrlDao;
import io.admin.modules.sys.dto.EquipmentDTO;
import io.admin.modules.sys.dto.FileUrlDTO;
import io.admin.modules.sys.entity.EquipmentEntity;
import io.admin.modules.sys.entity.EquipmentEntityPage;
import io.admin.modules.sys.entity.FileUrlEntity;
import io.admin.modules.sys.entity.ProjectEntity;
import io.admin.modules.sys.entity.UserEntity;
import io.admin.modules.sys.entity.UserEntityPage;
import io.admin.modules.sys.service.EquipmentService;
import cn.hutool.core.util.StrUtil;
import io.admin.modules.sys.service.FileUrlService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 设备表
 *
 * @author Mark sunlightcs@gmail.com
 * @since 1.0.0 2024-07-06
 */
@Service
public class EquipmentServiceImpl extends CrudServiceImpl<EquipmentDao, EquipmentEntity, EquipmentDTO> implements EquipmentService {
    @Autowired
    private EquipmentDao equipmentDao;

    @Autowired
    private FileUrlService  fileUrlService;

    @Override
    public PageData page(Map<String, Object> params){
        //转换成like
        paramsToLike(params, "name");
        //分页
        IPage<EquipmentEntity> page = getPage(params,  "", false);
        //查询
        return new PageData(getLineList(params),page.getTotal());
    }

    @Override
    public  List<EquipmentEntityPage>  getLineList(Map<String, Object> params){
        List<EquipmentEntityPage> list = equipmentDao.getList(params);
        if(!CollectionUtil.isEmpty(list)){
            for (EquipmentEntityPage p:list){
                params =new HashMap<>();
                params.put("keysId",String.valueOf(p.getId()));
                List<FileUrlDTO> listPro = fileUrlService.list(params);
                p.setList(listPro);
            }
        }
        return  list;
    }


    @Override
    public QueryWrapper<EquipmentEntity> getWrapper(Map<String, Object> params){
        String id = (String)params.get("id");
        String lindId = (String)params.get("lineId");
        QueryWrapper<EquipmentEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StrUtil.isNotBlank(id), "id", id);
        wrapper.eq(StrUtil.isNotBlank(lindId), "line_id", lindId);
        return wrapper;
    }
    @Override
    public List<EquipmentEntityPage> listByProject(Map<String, Object> params){
        return getLineList(params);
    }

    @Override
    public Integer getLineCount(Long lineId,Long projectId){
        Map<String,Object> param = new HashMap<>();
        param.put("lineId",lineId);
        param.put("projectId",projectId);
      return equipmentDao.getLineCount(param);
    }

    @Override
    public Integer getRepeatByEquipment(EquipmentDTO equipmentDTO){
        Map<String,Object> param = new HashMap<>();
        param.put("lineId",equipmentDTO.getLineId());
        param.put("projectId",equipmentDTO.getProjectId());
        param.put("name",equipmentDTO.getName());
        return equipmentDao.getLineCount(param);
    }

    @Override
    public EquipmentDTO findEqByParam(EquipmentDTO equipmentDTO){
    return equipmentDao.findEqByParam(equipmentDTO);
    }
    @Override
    public  Integer getSortEnd(EquipmentDTO equipmentDTO){
        EquipmentEntity equipmentDataThis =ConvertUtils.sourceToTarget(equipmentDTO, EquipmentEntity.class);
        return equipmentDao.getSortEnd(equipmentDataThis);
    }

    //开始修改上移动数据
    //获取最新的排序
   /* @Override
    public  Integer getSortEnd()*/
    public  Map<String,Object> getIdUpByThisSortId(EquipmentDTO equipmentDTO){
        EquipmentEntity equipmentDataThis =ConvertUtils.sourceToTarget(equipmentDTO, EquipmentEntity.class);
        //获取到上移的上移的数据
        Map<String,Object> retMap =new HashMap<>();
        EquipmentEntity equipmentData = equipmentDao.getIdUpByThisSortId(equipmentDataThis);
       //开始修改对应的数据
        if(null == equipmentData){
            retMap.put("code","4011");
            retMap.put("msg","你上移的数据已经是第一个了 不可以在上移了");
            return retMap;
        }
        //开始修改两条数据的排序

        Integer sortThis = equipmentDTO.getSort();
        equipmentDataThis.setSort(equipmentData.getSort());
        baseDao.updateById(equipmentDataThis);
        equipmentData.setSort(sortThis);
        baseDao.updateById(equipmentData);
        return null;
    }
    public  Map<String,Object>  getIdDownByThisSortId(EquipmentDTO equipmentDTO){
        EquipmentEntity equipmentDataThis =ConvertUtils.sourceToTarget(equipmentDTO, EquipmentEntity.class);
        //获取到上移的上移的数据
        Map<String,Object> retMap =new HashMap<>();
       EquipmentEntity equipmentData = equipmentDao.getIdDownByThisSortId(equipmentDataThis);
        //开始修改对应的数据
        if(null == equipmentData){
            retMap.put("code","4011");
            retMap.put("msg","你下移的数据已经是第最后一个了，不可以在下移了");
            return retMap;
        }
        //开始修改两条数据的排序
        Integer sortThis = equipmentDataThis.getSort();
        equipmentDataThis.setSort(equipmentData.getSort());
        baseDao.updateById(equipmentDataThis);

        equipmentData.setSort(sortThis);
        baseDao.updateById(equipmentData);
        return null;
    }







}